<script type="text/javascript">
$(function(){
	var pg = App.office.workEdit.ui, pp = pg.params || {};

	var frm = {
		title: pg.seek('title'),
		type: pg.seek('type'),
		priority: pg.seek('priority'),
		memo: pg.seek('memo'),
		planStartDate: pg.seek('planStartDate'),
		planEndDate: pg.seek('planEndDate'),
		actualStartDate: pg.seek('actualStartDate'),
		actualEndDate: pg.seek('actualEndDate'),
		owner: pg.seek('owner'),
		preWork: pg.seek('preWork'),
		doc: pg.seek('doc'),
		url: pg.seek('url'),
		progress: pg.seek('progress'),
		status: pg.seek('status'),
		flow: pg.seek('flow')
	};
	var dp = {
		prevText: "<上一月",
		nextText: "下一月>",
		dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true,
		inline: true
	}
	frm.planStartDate.datepicker(dp);
	frm.planEndDate.datepicker(dp);
	frm.actualStartDate.datepicker(dp);
	frm.actualEndDate.datepicker(dp);
	
	frm.type.mac('combo', {
		width: 121,
		boxWidth: 122,
		readOnly: 1,
		data: Cache.office.work.type
	});
	frm.status.mac('combo', {
		width: 121,
		boxWidth: 122,
		readOnly: 1,
		data: Cache.office.work.status
	}).disable().val(0);

	var preGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'title', title: '前置任务名', width: 400
		},{
			field: 'planEndDate', title: '计划完成时间', width: 120
		}],
		loader: {
			url: App.office.ctx + '/work/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.preWork.mac('combo', {
		keyField: 'id',
		displayField: 'title',
		multiSelect: true,
		width: 296,
		boxWidth: 600,
		boxHeight: 300,
		boxEl: preGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//frm.preWork.text(r ? r.name : '');
		}
	});
	frm.preWork.mac('combo_grid', {});
	
	var flowGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'name', title: '工作流名称', width: 400
		}],
		loader: {
			url: App.soagov.ctx + '/svc/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				catalog: 'office.workflow'//, type: Cache.soagov.svc.TYPE_WORK
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.flow.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		width: 296,
		boxWidth: 430,
		boxHeight: 300,
		boxEl: flowGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//frm.preWork.text(r ? r.name : '');
		}
	});
	frm.flow.mac('combo_grid', {});
	
	var userGd = $('<div class="grid"></div>').mac('grid', {
		key: 'id',
		cols : [{
			field: 'code', title : '编号', width: 120
		},{
			field: 'name', title : '姓名', width: 240
		}],
		loader: {
			url: App.auth.ctx + '/sys/user/list.do',
			params: { pageNo: 1, pageSize: 50 },
			autoLoad: true
		},
		pagerLength: 10
	});
	frm.owner.mac('combo', {
		keyField: 'id',
		displayField: 'name',
		multiSelect: false,
		width: 121,
		boxWidth: 400,
		boxHeight: 300,
		boxEl: userGd,
		onSelect: function(me, sk, sd){
			//var r = sd[0];
			//frm.owner.text(r ? r.name : '');
		}
	});
	frm.owner.mac('combo_grid', {});

	function getValues(){
		return {
			id: pp.id,
			parentId: pp.parentId,
			title: frm.title.val(),
			type: frm.type.val(),
			priority: frm.priority.val(),
			memo: frm.memo.val(),
			planStartDate: frm.planStartDate.val(),
			planEndDate: frm.planEndDate.val(),
			actualStartDate: frm.actualStartDate.val(),
			actualEndDate: frm.actualEndDate.val(),
			ownerId: frm.owner.val(),
			preWorkIds: frm.preWork.val().join(','),
			url: frm.url.val(),
			progress: frm.progress.val(),
			status: frm.status.val(),
			flowId: frm.flow.val(),
			flowExeId: pp.flowExeId,
			flowId2: pp.flowId2,
			flowExeId2: pp.flowExeId2
		};
	}
	function setValues(o){
		pg.seek('title').html(o.id? '编辑组件':'添加组件');
		frm.title.val(o.title);
		frm.type.val(o.type);
		frm.priority.val(o.priority * 1);
		frm.memo.val(o.memo);
		frm.planStartDate.val(o.planStartDate);
		frm.planEndDate.val(o.planEndDate);
		frm.actualStartDate.val(o.actualStartDate);
		frm.actualEndDate.val(o.actualEndDate);
		frm.owner.val(o.owner);
		frm.preWork.val(o.preWorks);
		frm.doc.attr('href', o.url);
		frm.url.val(o.url);
		frm.progress.val(o.progress || 0);
		frm.status.val(o.status || 0);
		// 如果当前任务是一个流程中的子任务，则不可再选流程
		// 如果子任务也希望发起一个流程，则应作为子任务嵌套在主任务的流程中
		// 否则这个流程实际上独立于主流程，主流程不会等待此流程执行完成。
		// 如果用户希望建立一个不影响主流程的子流程，则单独发起带流程的任务即可
		if (o.flowId2) {
			frm.flow.disable();
		} else {
			frm.flow.val(o.flow);
		}
	}
	function init(d){
		setValues(d);
		var s = d.status, S = Cache.office.work;
		// 状态为草稿时可以提交（启动流程流转）
		if (s == S.STATUS_DRAFT) {
			pg.seek('submit').click(function(){
				var pms = getValues();
				if (!validate(pms)) {
					return;
				}
				mac.confirm('确定要启动此任务吗？', function(){
					$.post(App.office.ctx + '/work/submit.do', pms, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pg.close();
							var ui = ro.data.ui;
							if (ui && ui.data.ownerId == App.logon.id) {
								var a = ui.code.split('/');
								var b = {
									ctx: a[0],
									action: a[1],
									params: ui.data
								}
								if (App.schemes[App.scheme][b.ctx][b.action]) {
									App.open(b);
								} else {
									App.show(b, pg.parent());
								}
							}
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				});
				return false;
			}).show();
		}
		// 状态未排期、已取消、已完成时可以关闭
		if (s == S.STATUS_PENDING || s == S.STATUS_CANCEL || s == S.STATUS_DONE) {
			pg.seek('close').click(function(){
				mac.confirm('确定要关闭此任务吗？关闭后此任务将不再显示！', function(){
					$.get(App.office.ctx + '/work/close.do', {
						id: pp.id
					}, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pg.close();
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				});
				return false;
			}).show();
		}
		// 状态为未启动、进行中时可以取消
		if (s == S.STATUS_PENDING || s == S.STATUS_RUNNING) {
			pg.seek('cancel').click(function(){
				mac.confirm('确定要取消此任务吗？', function(){
					$.get(App.office.ctx + '/work/cancel.do', {
						id: pp.id
					}, function(rs){
						var ro = mac.eval(rs);
						if (ro.success) {
							pg.close();
							return;
						}
						mac.alert(ro.data, ro.params);
					});
				});
				return false;
			}).show();
		} else {
			frm.status.disable();
		}
	}
	
	function validate(p){
		var x = App.validate1, v = App.office.validation, m = pg.seek('msg');
		if (p.planStartDate && p.planEndDate && p.planStartDate > p.planEndDate) {
			m.text('计划结束时间不得早于计划开始时间');
			return;
		}
		if (p.actualStartDate && p.actualEndDate && p.actualStartDate > p.actualEndDate) {
			m.text('实际结束时间不得早于实际开始时间');
			return;
		}
		var b = x(v.work.title, frm.title, m, '任务名') && x(v.work.ownerId, frm.owner, m, '负责人')
			&& x(v.work.type, frm.type, m, '任务类型') && x(v.work.priority, frm.priority, m, '优先级');
		return b;
	}
	pg.seek('ok').click(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
		$.post(App.office.ctx + '/work/save.do', pms, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				if (pg.callback) {
					pg.callback();
				}
				pg.close();
				return;
			}
			pg.seek('msg').html(mac.msg(ro.data, ro.params));
		});
		return false;
	});
	pg.seek('back').click(function(){
		pg.close();
	});

	if (pp.id) {
		$.get(App.office.ctx + '/work/get.do', {
			id: pp.id
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				init(ro.data);
				return;
			}
			mac.alert(ro.data, ro.params);
		});
	} else {
		setValues({
			owner: App.logon,
			priority: 3,
			progress: 0,
			status: 0
		});
	}
});
</script>
<div class="form2">
	<div class="item w2">
		<div class="label">任务名</div>
		<input type="text" name="title" maxlength="128" minlength="8" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">任务类型</div>
		<div class="combo fl" name="type"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">优先级</div>
		<input type="number" name="priority" min="1" max="5" step="1" value="3" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item w2">
		<div class="label">工作流</div>
		<div class="combo fl" name="flow"></div>
		<div class="clear"></div>
	</div>
	<div class="item w2">
		<div class="label">前置任务</div>
		<div class="combo fl" name="preWork"></div>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item w2">
		<div class="label">
			<a href="#" target="_blank" name="doc">文档链接（点击前往）</a>
		</div>
		<input type="text" name="url" maxlength="1024" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">负责人</div>
		<div class="combo fl" name="owner"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item">
		<div class="label">计划开始时间</div>
		<input type="text" name="planStartDate" readOnly="true" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">计划结束时间</div>
		<input type="text" name="planEndDate" readOnly="true" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">状态：</div>
		<div class="combo fl" name="status" readOnly="true"></div>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item">
		<div class="label">实际开始时间</div>
		<input type="text" name="actualStartDate" readOnly="true" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">实际结束时间</div>
		<input type="text" name="actualEndDate" readOnly="true" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<div class="label">进度</div>
		<input type="number" name="progress" min="0" max="100" step="5" value="0" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="item">
		<div class="msg" name="msg"></div>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	<div class="bottom">
		<button name="submit" class="hidden">提交任务</button>
		<button name="cancel" class="hidden">取消任务</button>
		<button name="close" class="hidden">关闭任务</button>
		<button name="ok">保存</button>
		<button name="back">返回</button>
	</div>
</div>